
import { RouteLocationNormalized } from 'vue-router'

interface TagView extends Partial<RouteLocationNormalized> {
  title: string
  meta: {
    title: string
    [key: string]: any
  }
}

interface TagsViewState {
  visitedViews: TagView[]
  cachedViews: string[]
  iframeViews: TagView[]
  currentTag: null
}

export const useTagsViewStore  = defineStore('tags-view', {
  state: (): TagsViewState => ({
    visitedViews: [],
    cachedViews: [],
    iframeViews: [],
    currentTag: null
  }),
  actions: {
    setCurrentTag(view: any) {
      this.currentTag = view  // 保存当前标签信息
      // console.log('this.currentTag', this.currentTag)
    },
    addView(view: any) {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    addIframeView(view: any) {
      if (this.iframeViews.some(v => v.path === view.path)) return
      this.iframeViews.push({
        ...view,
        title: view.meta?.title || 'no-name'
      })
    },
    addVisitedView(view: any) {
      if (this.visitedViews.some(v => v.path === view.path)) return
      this.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta.title || 'no-name'
        })
      )
      console.log('this.visitedViews',this.visitedViews)
    },
    addCachedView(view: any) {
      if (this.cachedViews.includes(view.name as string)) return
      if (!view.meta?.noCache) {
        this.cachedViews.push(view.name as string)
        console.log('缓存视图：', this.cachedViews)
      }
    },
    delView(view: any) {
      return new Promise<{ visitedViews: TagView[], cachedViews: string[] }>(resolve => {
        this.delVisitedView(view)
        this.delCachedView(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    delVisitedView(view: any) {
      return new Promise<TagView[]>(resolve => {
        this.visitedViews = this.visitedViews.filter(v => v.path !== view.path)
        this.iframeViews = this.iframeViews.filter(item => item.path !== view.path)
        resolve([...this.visitedViews])
      })
    },
    delIframeView(view: any) {
      return new Promise<TagView[]>(resolve => {
        this.iframeViews = this.iframeViews.filter(item => item.path !== view.path)
        resolve([...this.iframeViews])
      })
    },
    delCachedView(view: any) {
      return new Promise<string[]>(resolve => {
        const index = this.cachedViews.indexOf(view.name as string)
        if (index > -1) {
          this.cachedViews.splice(index, 1)
        }
        resolve([...this.cachedViews])
      })
    },
    delOthersViews(view: any) {
      return new Promise<{ visitedViews: TagView[], cachedViews: string[] }>(resolve => {
        this.delOthersVisitedViews(view)
        this.delOthersCachedViews(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    delOthersVisitedViews(view: any) {
      return new Promise<TagView[]>(resolve => {
        this.visitedViews = this.visitedViews.filter(v => v.meta?.affix || v.path === view.path)
        this.iframeViews = this.iframeViews.filter(item => item.path === view.path)
        resolve([...this.visitedViews])
      })
    },
    delOthersCachedViews(view: any) {
      return new Promise<string[]>(resolve => {
        const index = this.cachedViews.indexOf(view.name as string)
        if (index > -1) {
          this.cachedViews = this.cachedViews.slice(index, index + 1)
        } else {
          this.cachedViews = []
        }
        resolve([...this.cachedViews])
      })
    },
    delAllViews() {
      return new Promise<{ visitedViews: TagView[], cachedViews: string[] }>(resolve => {
        this.delAllVisitedViews()
        this.delAllCachedViews()
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    delAllVisitedViews() {
      return new Promise<TagView[]>(resolve => {
        const affixTags = this.visitedViews.filter(tag => tag.meta?.affix)
        this.visitedViews = affixTags
        this.iframeViews = []
        resolve([...this.visitedViews])
      })
    },
    delAllCachedViews() {
      return new Promise<string[]>(resolve => {
        this.cachedViews = []
        resolve([...this.cachedViews])
      })
    },
    updateVisitedView(view: TagView) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = { ...v, ...view }
          break
        }
      }
    },
    delRightTags(view: any) {
      return new Promise<TagView[]>(resolve => {
        const index = this.visitedViews.findIndex(v => v.path === view.path)
        if (index === -1) return
        this.visitedViews = this.visitedViews.filter((item, idx) => {
          if (idx <= index || item.meta?.affix) return true
          const i = this.cachedViews.indexOf(item.name as string)
          if (i > -1) this.cachedViews.splice(i, 1)
          if (item.meta?.link) {
            const fi = this.iframeViews.findIndex(v => v.path === item.path)
            this.iframeViews.splice(fi, 1)
          }
          return false
        })
        resolve([...this.visitedViews])
      })
    },
    delLeftTags(view: any) {
      return new Promise<TagView[]>(resolve => {
        const index = this.visitedViews.findIndex(v => v.path === view.path)
        if (index === -1) return
        this.visitedViews = this.visitedViews.filter((item, idx) => {
          if (idx >= index || item.meta?.affix) return true
          const i = this.cachedViews.indexOf(item.name as string)
          if (i > -1) this.cachedViews.splice(i, 1)
          if (item.meta?.link) {
            const fi = this.iframeViews.findIndex(v => v.path === item.path)
            this.iframeViews.splice(fi, 1)
          }
          return false
        })
        resolve([...this.visitedViews])
      })
    }
  }
})
